﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace PapnKukn.Silverlight.SilverKnight
{
    public class Field : Canvas
    {
        #region Declarations
        private int r = 0;
        private int c = 0;
        private bool isLast = false;
        private bool isAllowed = true;
        private bool isSelected = false;
        private Rectangle region = null;
        private TextBlock score = null;
        #endregion

        #region Constructor
        public Field(int row, int column)
        {
            r = row;
            c = column;
            this.Width = 20;
            this.Height = 20;
            Render();
        }
        #endregion

        #region Properties
        public int Row
        {
            get { return r; }
            set { r = value; }
        }

        public int Column
        {
            get { return c; }
            set { c = value; }
        }

        public bool IsLast
        {
            get
            {
                return this.isLast;
            }
            set
            {
                this.isLast = value;
            }
        }

        public bool IsSelected
        {
            get
            {
                return this.isSelected;
            }
            set
            {
                this.isSelected = value;
            }
        }

        public bool IsAllowed
        {
            get
            {
                return this.isAllowed;
            }
            set
            {
                this.isAllowed = value;
            }
        }

        public int Score
        {
            get
            {
                int result = 0;
                if (Int32.TryParse(score.Text, out result))
                {
                    return result;
                }
                else
                {
                    return -1;
                }
            }
            set
            {
                score.Text = value.ToString();
            }
        }
        #endregion

        #region Methods
        public void Render()
        {
            //Clear previous graphics
            this.Children.Clear();

            //Create background rectangle
            Rectangle rectangle = new Rectangle();
            rectangle.Width = this.Width;
            rectangle.Height = this.Height;
            rectangle.Stroke = (Brush)App.Current.Resources["ForegroundBrush"];
            if (r % 2 == 1 ^ c % 2 == 1) //XOR
            {
                rectangle.Fill = (Brush)App.Current.Resources["LightBrush"];
            }
            else
            {
                rectangle.Fill = (Brush)App.Current.Resources["DarkBrush"];
            }
            this.Children.Add(rectangle);

            //Create clickable field
            region = new Rectangle();
            region.Width = this.Width - 2;
            region.Height = this.Height - 2;
            region.Margin = new Thickness(1);
            region.Cursor = Cursors.Hand;
            region.Fill = (Brush)App.Current.Resources["EmptyBrush"]; //Brush with 0x01 opacity to make region clickable
            //region.MouseEnter += new MouseEventHandler(region_MouseEnter); //Works in Silverlight 2.0 beta2 but not in release
            //region.MouseLeave += new MouseEventHandler(region_MouseLeave);
            //region.MouseLeftButtonDown += new MouseButtonEventHandler(region_MouseLeftButtonDown);
            this.Children.Add(region);

            //Create score field
            score = new TextBlock();
            score.Width = this.Width - 2;
            score.Height = this.Height - 2;
            score.Margin = new Thickness(1);
            score.TextAlignment = TextAlignment.Center;
            score.TextWrapping = TextWrapping.NoWrap;
            score.FontSize = 12;
            this.Children.Add(score);
        }

        public void SetColor(string name)
        {
            if (region != null)
            {
                region.Fill = (Brush)App.Current.Resources[name];
            }
        }
        #endregion

        #region Events
        public void TriggerMouseEnter(object sender, MouseEventArgs e)
        {
            region_MouseEnter(sender, e);
        }

        public void TriggerMouseLeave(object sender, MouseEventArgs e)
        {
            region_MouseLeave(sender, e);
        }

        public void TriggerMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            region_MouseLeftButtonDown(sender, e);
        }

        protected void region_MouseEnter(object sender, MouseEventArgs e)
        {
            if (region != null && !isSelected)
            {
                OnMouseOver(new RoutedEventArgs());

                if (isAllowed)
                {
                    region.Fill = (Brush)App.Current.Resources["GreenBrush"];
                }
                else
                {
                    region.Fill = (Brush)App.Current.Resources["RedBrush"];
                }
            }
        }

        protected void region_MouseLeave(object sender, MouseEventArgs e)
        {
            if (region != null && !isSelected)
            {
                region.Fill = (Brush)App.Current.Resources["EmptyBrush"];
            }
        }

        protected void region_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            if (region != null && !isSelected)
            {
                OnSelected(new RoutedEventArgs());

                if (isAllowed)
                {
                    isSelected = true;
                    region.Cursor = Cursors.Arrow;
                    region.Fill = (Brush)App.Current.Resources["VioletBrush"];
                }
            }
        }
        #endregion

        #region Bubble events
        public event RoutedEventHandler Selected;
        protected virtual void OnSelected(RoutedEventArgs e)
        {
            if (Selected != null)
            {
                Selected(this, e);
            }
        }

        public event RoutedEventHandler MouseOver;
        protected virtual void OnMouseOver(RoutedEventArgs e)
        {
            if (MouseOver != null)
            {
                MouseOver(this, e);
            }
        }
        #endregion
    }
}
